package com.neusoft.service.impl;

import com.neusoft.feign.BusinessFeignClient;
import com.neusoft.feign.FoodFeignClient;
import com.neusoft.mapper.CartMapper;
import com.neusoft.po.Business;
import com.neusoft.po.Cart;
import com.neusoft.po.CommonResult;
import com.neusoft.po.Food;
import com.neusoft.service.CartService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

	@Autowired
	private CartMapper cartMapper;
	@Autowired
	private FoodFeignClient foodFeignClient;
	@Autowired
	private BusinessFeignClient businessFeignClient;

	@Override
	public List<Cart> listCart(Cart cart) {
		List<Cart> carts=cartMapper.listCart(cart);
		for(Cart c:carts){
			int businessId=c.getBusinessId();
			int foodId=c.getFoodId();

			CommonResult<Food> food=foodFeignClient.getFoodById(businessId,foodId);
			if(food.getCode()==200) c.setFood(food.getResult());
			else c.setFood(null);

			CommonResult<Business> business=businessFeignClient.getBusinessById(businessId);
			if(business.getCode()==200) c.setBusiness(business.getResult());
			else c.setBusiness(null);
		}
		return carts;
	}

	@Override
	public int saveCart(Cart cart) {
		return cartMapper.saveCart(cart);
	}

	@Override
	public int updateCart(Cart cart) {
		return cartMapper.updateCart(cart);
	}

	@Override
	public int removeCart(Cart cart) {
		return cartMapper.removeCart(cart);
	}
}
